<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\RedisService;
use App\Models\Gift;
use App\Models\LotteryResult;
use App\Models\User;
use Log;

class IndexController extends Controller
{
    public function index()
    {    	
        $app = app('wechat.official_account');
        $share = config('wxShare');
    	return view('lottery.index',compact('app','share'));
    }

    public function game()
    {
    	return view('lottery.game');
    }

    public function draw(Request $request)
    {
    	$rs['status'] = false;
        $wxuser =  session('wechat.oauth_user.default'); // 公众号
        //dd($wxuser);
        $openid = $wxuser->id;
    	
        // 是否在白名单里面
        $wl = RedisService::get_whitelist($openid);
        if(!$wl) {
            // 判断今天还有没有抽奖机会
            $key = 'h5:drawtimes:'.date('Ymd').':'.$openid;
            $times = RedisService::get($key);

            if(!isset($times)) {
                $timestamp = strtotime("+1 day");
                $dts = config('app.draw_times');
                RedisService::set($key,$dts,$timestamp);
               
                } elseif($times<1) {
                $rs['msg'] = '已经没有抽奖机会了';
                return response()->json($rs);   
            }    
        }


    	
      
    	
    	$maxKey = 'h5:gift:4';
    	$max    = RedisService::get($maxKey);
    	
    	$maxData = json_decode($max);
        $maxNum  = object_get($maxData,'num','60100');
    	$num     = mt_rand(1,$maxData->num);    	
    	$gift    = $this->getGift($num);
    	$data    = json_decode($gift);
    	//dd($data);
    	if($data) {
    		

    		$info['openid']       = $openid;
    		$info['nickname']     = array_get($wxuser,'nickname');
    		$info['headimgurl']   = array_get($wxuser,'avatar');
    		$info['gift_id']      = $data->id;
    		$info['gift_name']    = $data->name;
    		$info['gift_content'] = $data->content;
            $info['gift_type']    = object_get($data,'type',1);
    		$info['code'] = date('YmdHis').mt_rand(10000,99999);
    		$result = LotteryResult::create($info);  		
    		$key = 'h5:drawtimes:'.date('Ymd').':'.$openid;
    		RedisService::decr($key);
    		$key = 'h5:gift:num:'.$data->id;    		
    		RedisService::decr($key);

            // 中奖数+1
            RedisService::set_gifted_num('total');  
            RedisService::set_gifted_num(date('Ymd'));
    		$rs['status'] = true;
            $data->result_id = $result->id;
    		$rs['data'] = $data;
            
    		return response()->json($rs);
    	}
    	return response()->json($rs);
    }

    public function saveInfo(Request $request)
    {
    	$rs['status'] = false;
        $wxuser =  session('wechat.oauth_user.default'); // 公众号
    	if(empty($request->gift_code)) {
    		$rs['msg'] = '奖品code错误';
    		return response()->json($rs);
    	}



    	$key = 'h5:gift:'.$request->gift_id;
    	$info = RedisService::get($key);
    	$info = json_decode($info);
    	//dd($info);
    	if(object_get($info,'code') != $request->gift_code) {
    		$rs['msg'] = '奖品code错误';
    		return response()->json($rs);
    	}

        try {
            $openid = $wxuser->id;
            $where['openid']    = $openid;
            $where['id'] = $request->result_id;
            $result = LotteryResult::where($where)->first(); 
            if(!$result) {
                $rs['msg'] = '信息错误';
                return response()->json($rs);
            }
            $result->truename     = $request->truename;
            $result->mobile       = $request->mobile;
            $result->address      = $request->address;
            $result->is_converted = 1;
            $flag = $result->save();    
            if($flag) {
                RedisService::set_gifted_num(date('Ymd'));
                RedisService::set_gifted_num();
                $rs['status'] = true;
                $rs['msg'] = '操作成功';
                return response()->json($rs);
            }    
        } catch (\Exception $e) {
            $rs['msg'] = $e->getMessage();
            return response()->json($rs);
        }
    	$rs['msg'] = '操作失败';
    	return response()->json($rs);
    }

    public function saveCoupon(Request $request)
    {
        $rs['status'] = false;
        dd(1);
        Log::info('saveCoupon:'.$request->result_id);
        $where['id'] = $request->result_id;
        $result = LotteryResult::where($where)->first(); 
        if(!$result) {
            $rs['msg'] = '信息错误';
            return response()->json($rs);
        }
       
        $result->is_converted = 1;
        $flag = $result->save();    
        if($flag) {
            
            $rs['status'] = true;
            $rs['msg'] = '操作成功';
            return response()->json($rs);
        }
        return response()->json($rs);
    }

   

    private function getGift($num)
    {
    	$key = 'h5:gift:';
    	$numkey = 'h5:gift:num:';
    	$gift = '';
    	$gift_num = 0;

    	// 一共68个实物奖品
    	// 一等奖
    	if($num<4) {    		
    		$gift     = RedisService::get($key.'1');
    		$gift_num = RedisService::get($numkey.'1');
    	} else if($num>3&&$num<9) {    		
    		$gift     = RedisService::get($key.'2');
    		$gift_num = RedisService::get($numkey.'2');
    	} else if($num>8&&$num<19) {    		
    		$gift     = RedisService::get($key.'3');
    		$gift_num = RedisService::get($numkey.'3');
    	} elseif($num>18&&$num<69){           
            $gift     = RedisService::get($key.'9');
            $gift_num = RedisService::get($numkey.'9');
        }else if($num>20068) {
    		// 京东券    		
    		$gift     = RedisService::get($key.'4');
    		$gift_num = RedisService::get($numkey.'4');
    	} else {
    		$rand = mt_rand(7,8);    		
    		$gift = RedisService::get($key.$rand);
    		$gift_num = RedisService::get($numkey.$rand);
    	}
    	//dd($key,$num);
    	if($gift_num<1) {
    		$gift = RedisService::get($key.'4');
    		
    	}
    	//dd($num);
    	return $gift;
    }

    public function myGift(Request $request) 
    {
    	$rs['status'] = false;
        $wxuser =  session('wechat.oauth_user.default');
    	$where['openid'] = $wxuser->id;
    	$list = LotteryResult::where($where)->get();
    	if($list) {
    		$rs['data']= $list;
    	}
    	return response()->json($rs);
    }

    public function times()
    {
        $rs['status'] = true;
        $wxuser =  session('wechat.oauth_user.default');
        $openid = $wxuser->id;

        $key = 'h5:drawtimes:'.date('Ymd').':'.$openid;
        $times = RedisService::get($key);

        if(!isset($times)) {
            $timestamp = strtotime("+1 day");
            $dts = config('app.draw_times');
            RedisService::set($key,$dts,$timestamp);
            $rs['data'] = $dts;
            return response()->json($rs);
        } elseif($times<1) {
            
            $rs['data'] = 0;
            $rs['msg'] = '已经没有抽奖机会了';
            return response()->json($rs);   
        }
        $rs['data'] = $times;
        return response()->json($rs);
    }

    public function test(Request $request)
    {
    	$rs['status'] = true;
        $wxuser =  session('wechat.oauth_user.default');
        $key = 'h5:drawtimes:'.date('Ymd').':'.$wxuser->id;
        $times = RedisService::get($key);
        $rs['data'] = (int)$times;
        return response()->json($rs);
    	
    }

}
